using System.Collections.Generic;
using System.Configuration;
using System.Data;

namespace NetMigrationTests.FunctionalTests
{
    public class DataFactory
    {
        protected string connectionString = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
        private readonly DB db;

        public DataFactory()
        {
            db = new DB(connectionString);
        }

        public List<string> GetColumns(string tableName)
        {
            var sql = string.Format("SELECT name from dbo.syscolumns where id=object_id(N'{0}')", tableName);
            var dt = db.ExecuteDataTable(sql);
            var colNames = new List<string>();
            foreach (DataRow row in dt.Rows)
            {
                colNames.Add(row[0].ToString());
            }
            return colNames;
        }

        public List<string> GetTableNames()
        {
            var tableNames = new List<string>();
            DataTable dt =
                db.ExecuteDataTable("SELECT t.name FROM sys.tables AS t");
            foreach (DataRow row in dt.Rows)
            {
                tableNames.Add(row[0].ToString());
            }
            return tableNames;
        }

        public void Drop(string tableName)
        {
            db.ExecuteCommand(string.Format("IF object_id('{0}','U') IS NOT NULL DROP TABLE [{0}];", tableName));
        }
    }
}